package com.kulian.Signalling.core.oss.service.impl;


import cn.hutool.core.io.FileUtil;
import com.kulian.Signalling.core.oss.properties.LocalProperties;
import com.kulian.Signalling.core.oss.service.OSSUnity;
import jakarta.annotation.Resource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class LocalServiceImpl implements OSSUnity {


    @Resource
    private LocalProperties localProperties;

    @Override
    public String upload(String path, File file) {

        if (!file.getParentFile().exists())
        {
            file.getParentFile().mkdirs();
        }
        if (!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return localProperties.getDomain() + path;
    }

    @Override
    public String upload(String path, InputStream inputStream) {

        FileUtil.writeFromStream(inputStream, path);

        return localProperties.getDomain() + path;
    }

    private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
    {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        if (!desc.exists())
        {
            desc.createNewFile();
        }
        return desc;
    }
}
